package com.sakura.mall.order.orderSetting.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.orderSetting.domain.entity.OrderSettingEntity;
import com.sakura.mall.order.orderSetting.domain.request.AddOrderSettingRequest;
import com.sakura.mall.order.orderSetting.domain.request.UpdateOrderSettingRequest;
import com.sakura.mall.order.orderSetting.domain.request.OrderSettingPageRequest;
import com.sakura.mall.order.orderSetting.domain.vo.OrderSettingVo;
import com.sakura.mall.order.orderSetting.mapper.OrderSettingMapper;
import com.sakura.mall.order.orderSetting.service.OrderSettingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单配置信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSettingEntity> implements OrderSettingService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderSettingRequest request) {
        // todo 其他校验逻辑
        OrderSettingEntity orderSetting = request.convert(OrderSettingEntity.class);
        this.save(orderSetting);
    }

    @Override
    public Page<OrderSettingVo> queryPage(OrderSettingPageRequest request) {
        IPage<OrderSettingEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderSettingEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderSettingVo> ret = qp.convert(entity -> entity.convert(OrderSettingVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderSettingRequest request) {
        OrderSettingEntity entity = request.convert(OrderSettingEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

